package com.zmkj.cn.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmkj.cn.sys.dto.SysLogDto;
import com.zmkj.cn.sys.entity.SysLog;
import com.zmkj.cn.sys.mapper.SysLogMapper;
import com.zmkj.cn.sys.service.ISysLogService;
import com.zmkj.cn.sys.vo.input.SysLogInputVo;
import com.zmkj.cn.sys.vo.query.SysLogQueryVo;
import com.zmkj.cn.comm.vo.PageInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class SysLogServiceImpl implements ISysLogService  {


    @Autowired
    private SysLogMapper sysLogMapper;



     /**
     * 新增修改方法
     * @param sysLogInputVo
     */
    @Override
    public SysLog saveOrUpdate(SysLogInputVo sysLogInputVo) {
        SysLog sysLog=new SysLog();
        BeanUtils.copyProperties(sysLogInputVo,sysLog);
        if(sysLogInputVo.getId()==null){
            sysLog= save(sysLog);
        }else{
            sysLog= update(sysLog);
        }
        return sysLog;
    }



    /**
     * 新增
     * @param sysLog
     */
    @Override
    public SysLog save(SysLog sysLog) {
       sysLogMapper.insertSelective(sysLog);
        return sysLog;
    }


     /**
     * 修改
     * @param sysLog
     */
    @Override
    public SysLog update(SysLog sysLog) {
        sysLogMapper.updateByPrimaryKeySelective(sysLog);
        return sysLog;
    }



     /**
     * 通过主键 查询对应对象
     * @param id
     * 
     */
    @Override
    public SysLog getByPrimary(Integer id) {
        SysLog sysLog= sysLogMapper.selectByPrimaryKey(id);
        return sysLog;
    }





    /**
     * 通过主键 查询对应对象DTO
     * @param id
     * 
     */
    @Override
    public SysLogDto getDtoByPrimary(Integer id) {
        SysLog sysLog= sysLogMapper.selectByPrimaryKey(id);
        SysLogDto  sysLogDto=new SysLogDto();
        BeanUtils.copyProperties(sysLog,sysLogDto);
        return sysLogDto;
    }


     /**
     * 根据主键删除对象
     * @param id
     */
     @Override
     public void deleteByPrimary(Integer id) {
        sysLogMapper.deleteByPrimaryKey(id);
     }

    /**
    * 分页查询
    * @param  pageInput 分页参数
    * @param sysLogQueryVo 查询条件
    * @return
    */
    @Override
    public Object getPage(PageInput pageInput,SysLogQueryVo sysLogQueryVo) {
        PageHelper.startPage(pageInput.getPageNum(), pageInput.getPageSize());
        List<SysLogDto> list=sysLogMapper.getPage(sysLogQueryVo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }


   /**
     * 根据对象查询
     * @param sysLog 根据对象查询
     */
     @Override
     public List<SysLog> getList(SysLog sysLog){
       return sysLogMapper.select(sysLog);
     }

     @Async
    @Override
    public void saveLog(String ip, String url, String code, Long reqTimes,String param,String reqId) {
        SysLog sysLog=new SysLog();
        sysLog.setReqIp(ip);
        sysLog.setReqUrl(url);
        sysLog.setReqTimes(reqTimes+"");
      /*  sysLog.setReqParam(param);*/
        sysLog.setReqId(reqId);
        sysLog.setReqCode(code);
        sysLogMapper.insertSelective(sysLog);
    }

}